<template>
  <Dialog
    :title="title"
    :model-value="visible"
    min-width="1280px"
    width="80%"
    max-height="600px"
    :before-close="closeForm"
  >
    <el-row>
      <el-col :span="6">
        <Descriptions
          labelAlign="right"
          key="datum"
          :column="1"
          :collapse="false"
          label-width="20%"
          width="100%"
          :data="dept"
          :schema="scheme"
        />
      </el-col>
      <el-col :span="18">
        <ContentWrap :title="dept.DeptName + (isEntry ? '入职' : '') + '员工列表'">
          <template #extr>
            <el-switch
              v-model="isEntry"
              active-text="显示入职员工"
              inactive-text="显示本公司员工"
            />
          </template>
          <empListView :unit-id="unitId" :dept-id="deptId" :is-entry="isEntry" :is-load="isLoad" />
        </ContentWrap>
      </el-col>
    </el-row>
    <empModel :id="empId" v-model:visible="empVisible" />
  </Dialog>
</template>

<script>
import * as deptApi from '@/api/unit/dept'
import { HrItemDic } from '@/config/publicDic'
import empName from '@/components/emp/src/empName.vue'
import empModel from '@/components/emp/src/empModel.vue'
import { Dialog } from '@/components/Dialog'
import { Descriptions } from '@/components/Descriptions'
import { dicItemName } from '@/components/base/'
import deptName from './deptName.vue'
import empListView from '@/components/emp/src/empListView.vue'
import { h } from 'vue'
export default {
  name: 'DeptView',
  components: {
    Descriptions,
    empModel,
    empListView,
    Dialog
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      HrItemDic,
      title: '部门信息',
      scheme: [],
      isEntry: true,
      empVisible: false,
      empId: null,
      dept: {},
      isUnit: false,
      unitId: null,
      deptId: null,
      isLoad: false
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        } else {
          this.isLoad = false
        }
      },
      immediate: true
    }
  },
  methods: {
    showEmp(empId) {
      this.empId = empId
      this.empVisible = true
    },
    intScheme() {
      const titleName = this.isUnit ? '单位' : '部门'
      const list = [
        {
          field: 'DeptName',
          label: titleName + '全称'
        },
        {
          field: 'ShortName',
          label: titleName + '简称'
        },
        {
          field: 'ParentId',
          label: '所属上级',
          slots: {
            default: () => {
              return h(deptName, {
                deptId: this.dept.ParentId
              })
            }
          }
        },
        {
          field: 'CompanyType',
          label: '说明'
        },
        {
          field: 'LeaderName',
          label: '负责人'
        }
      ]
      if (this.isUnit) {
        list.push({
          field: 'DeptTag',
          label: '部门分类',
          slots: {
            default: () => {
              return h(dicItemName, {
                dicId: HrItemDic.deptTag,
                values: this.dept.DeptTag
              })
            }
          }
        })
      }
      this.scheme = list
    },
    async reset() {
      const data = await deptApi.get(this.id)
      this.dept = data
      this.isUnit = data.IsUnit
      if (this.isUnit) {
        this.unitId = this.id
        this.deptId = null
        this.title = '查看单位:' + data.DeptName
      } else {
        this.unitId = data.UnitId
        this.deptId = [this.id]
        this.title = '查看部门:' + data.DeptName
      }
      this.intScheme()
      this.isLoad = true
    },
    closeForm() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
